using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DTLAutomateGUI.Controls.FormControls;

namespace DTLAutomateGUI.Controls.LayoutControls
{
	public partial class LayoutItemControl : UserControl, IDeleteable, IMoveable, Selectable
	{
		public LayoutItemControl()
		{
			InitializeComponent();
		}

		#region properties

		private CanvasMode currentCanvasMode;

		public CanvasMode CurrentCanvasMode
		{
			get { return currentCanvasMode; }
			set { currentCanvasMode = value; }
		}


		#endregion

		#region IMoveable Members

		private bool movable = true;

		public bool Movable
		{
			get { return movable; }
			set { movable = value; }
		}

		private RotateFlipType rotation;

		public RotateFlipType Rotation
		{
			get { return rotation; }
			set { rotation = value; }
		}

		// helper variables for moving
		protected Point origin;
		protected bool moving = false;

		protected void LayoutItemControl_MouseDown(object sender, MouseEventArgs e)
		{
			switch (this.CurrentCanvasMode)
			{
				case CanvasMode.Select:
					if (this.movable)
					{
						this.origin = new Point(e.X, e.Y);
						this.moving = true;
					}
					this.Selected = true;
					break;
				case CanvasMode.Delete:
					break;
				default:
					break;
			}
		}

		protected void LayoutItemControl_MouseUp(object sender, MouseEventArgs e)
		{
			this.moving = false;
		}

		protected void LayoutItemControl_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.moving)
			{
				this.Location = new Point(this.Location.X + e.X - this.origin.X, this.Location.Y + e.Y - this.origin.Y);
			}
		}

		#endregion

		#region IDeleteable Members

		public event DeleteControl deleteControl;

		public void deleteThis()
		{
			if (this.deleteControl != null) this.deleteControl(this);
		}

		#endregion

		#region Selectable Members

		public event SelectedEvent selectionPerformed;

		private bool selected;

		public bool Selected
		{
			get { return selected; }
			set
			{
				selected = value;
				if (this.selectionPerformed != null) this.selectionPerformed(new SelectEventArgs(this, (value ? SelectAction.Select : SelectAction.Deselect)));
				this.Invalidate();
			}
		}

		public void toggleSelected()
		{
			this.Selected = !(this.Selected);
		}

		#endregion

		private void LayoutItemControl_Paint(object sender, PaintEventArgs e)
		{
			if (this.Selected)
			{
				e.Graphics.DrawRectangle(new Pen(Color.Red, 4), 1, 1, this.Width - 4, this.Height - 4);
			}
		}

		
	}
}
